package com.wu.dao.impl;

import com.wu.bean.Leavingamessage;
import com.wu.dao.LeavingamessageDao;
import com.wu.model.LeavingamessageUserModel;

import java.util.List;

public class LeavingamesageDaoImpl extends BaseDao implements LeavingamessageDao {
    @Override
    public List<LeavingamessageUserModel> QueryLeavingamessageAll() {
        String sql="SELECT l.`id`, u.`username`,u.`email`,l.`content`,l.`time`  FROM leavingamessage l,USER u WHERE l.`messageperson`=u.`id` ORDER BY TIME DESC";
        return queryForList(LeavingamessageUserModel.class,sql);
    }


    @Override
    public List<LeavingamessageUserModel> QueryUsername(String Username) {
        String sql="SELECT leavingamessage.`id`, user.`username`,user.`email`,leavingamessage.`content`,leavingamessage.`time`  FROM leavingamessage,USER WHERE leavingamessage.`messageperson`=user.`id` AND user.`username`=?  ORDER BY TIME DESC";
        return queryForList(LeavingamessageUserModel.class,sql,Username);
    }

    @Override
    public List<LeavingamessageUserModel> QueryLeavingamessageLike(String like) {
        String sql="SELECT leavingamessage.`id`, user.`username`,user.`email`,leavingamessage.`content`,leavingamessage.`time`  \n" +
                "FROM leavingamessage,USER WHERE leavingamessage.`messageperson`=user.`id` \n" +
                " AND ( leavingamessage.`content` LIKE ? OR user.`username` LIKE ?)  ORDER BY TIME DESC";
        return queryForList(LeavingamessageUserModel.class,sql,"%"+like+"%","%"+like+"%");
    }

    @Override
    public int DeleteLeavingamessage(int id) {
        String sql="DELETE FROM leavingamessage WHERE id=?";
        return update(sql,id);
    }

    @Override
    public int AddLeavingamessage(Leavingamessage leavingamessage) {
        String sql="INSERT INTO leavingamessage(messageperson,content,TIME)VALUES(?,?,?)";
        return update(sql,leavingamessage.getMessageperson(),leavingamessage.getContent(),leavingamessage.getTime());
    }
}
